package io.israel.dan;

import lombok.extern.slf4j.Slf4j;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.quartz.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.Map;


@SpringBootApplication
public class IsraelDanApplication {

	public static void main(String[] args) {
		SpringApplication.run(IsraelDanApplication.class, args);
	}

	@Bean
	public JobDetail jobDetail() {
		return JobBuilder.newJob(SomeQuartzJob.class)
				.withIdentity("job1")
				.usingJobData("param1", "p1")
				.usingJobData("param2", "p2")
				.storeDurably()
				.build();
	}

	@Bean
	public Trigger trigger(JobDetail jobDetail) {
		return TriggerBuilder.newTrigger()
				.forJob(jobDetail)
				.withIdentity("trigger1")
				.withSchedule(
						CalendarIntervalScheduleBuilder
								.calendarIntervalSchedule()
								.withIntervalInSeconds(5)
				)
				.build();
	}
}

@Slf4j
class SomeQuartzJob implements Job {

	private DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");

	@Override
	public void execute(JobExecutionContext context) {
		Map<String, Object> map = context.getJobDetail().getJobDataMap();
		log.info("Fire Time :{} Using : {}", dateTimeFormatter.print(context.getFireTime().getTime()), map);
	}
}
